//% color="#1E90FF" iconWidth=50 iconHeight=40
namespace opencv {

    //% block="读取图片文件:[img_file] 设置[Processing] 并返回[img]" blockType="command"
    //% img_file.shadow="string" img_file.defl="Mind+.png"
    //% Processing.shadow="dropdown" Processing.options="MODEL"
    //% img.shadow="normal" img.defl="img"
    export function img_read(parameter: any, block: any) {
        let file_path = parameter.img_file.code;
        let Processing = parameter.Processing.code;
        let img = parameter.img.code;
        Generator.addImport(`import cv2\nimport numpy\nimport os\nimport matplotlib.pyplot`);
        Generator.addCode(`
${img} = cv2.imread(${file_path},cv2.${Processing})
`);
    }
    
    //% block="保存图像[img]到[img_file]" blockType="command"
    //% img.shadow="normal" img.defl="img"
    //% img_file.shadow="string" img_file.defl="Mind+.png"
    export function save_image(parameter: any, block: any) {
        let img = parameter.img.code;
        let file_path = parameter.img_file.code;
        Generator.addCode(`
cv2.imwrite( ${file_path}, ${img})
`);
    }

    //% block="图像:[img] 统一尺寸[size] 返回[output]" blockType="command"
    //% img.shadow="normal" img.defl="img"
    //% size.shadow="normal" size.defl="(100,100)"
    //% output.shadow="normal" output.defl="img"
    export function resize_image(parameter: any, block: any) {
        let file_path = parameter.img.code;
        let size = parameter.size.code;
        let img = parameter.output.code;
        Generator.addCode(`
${img} = cv2.resize(${file_path},${size})
`);
    }


    //% block="图像:[img]卷积操作输出为:[img_out]卷积核:[Matrix]" blockType="command"
    //% img.shadow="normal" img.defl="img"
    //% img_out.shadow="normal" img_out.defl="img"
    //% Matrix.shadow="normal" Matrix.defl="[1, 0, -1], [1, 0, -1], [1, 0, -1]"
    export function convolve_image(parameter: any, block: any) {
        let img = parameter.img.code;
        let img_out = parameter.img_out.code;
        let Matrix = parameter.Matrix.code;
        Generator.addCode(`
kernel = numpy.array([${Matrix}])
${img_out} =cv2.filter2D(${img}, -1, kernel)
`);
    }

    //% block="图像:[img]池化操作像素缩小[Multiples]倍输出为:[img_out]" blockType="command"
    //% img.shadow="normal" img.defl="img"
    //% Multiples.shadow="normal" Multiples.defl="2"
    //% img_out.shadow="normal" img_out.defl="img"
    export function pool_image(parameter: any, block: any) {
        let img = parameter.img.code;
        let Multiples = parameter.Multiples.code;
        let img_out = parameter.img_out.code;
        Generator.addCode(`
${img_out} = cv2.resize(${img},(${img}.shape[1]//${Multiples}, ${img}.shape[0]//${Multiples}))
`);
    }    
    
    //% block="对图像:[img]展开成一维向量 输出为:[flattened_image]" blockType="command"
    //% img.shadow="normal" img.defl="img"
    //% flattened_image.shadow="normal" flattened_image.defl="flattened_img"
    export function flatten(parameter: any, block: any) {
        let img = parameter.img.code;
        let flattened_image = parameter.flattened_image.code;
        Generator.addCode(`
${flattened_image} = ${img}.flatten()
`);
    }
        
    //% block="输出:[print]" blockType="command"
    //% print.shadow="normal" print.defl="img.shape"
    export function print(parameter: any, block: any) {
        let print = parameter.print.code;
        Generator.addCode(`
print(${print})
`);
    }
}